package de.gameduell.framework.facebook.app.popups {
	import de.gameduell.framework.facebook.app.pyramide.Brick;
	import de.gameduell.framework.facebook.app.pyramide.BrickCondition;
	import de.gameduell.framework.facebook.app.pyramide.BrickSelectItem;

	import fl.transitions.TweenEvent;
	import fl.transitions.easing.Regular;

	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.gui.animation.GDTween;
	import de.gameduell.framework.gui.button.Button;
	import de.gameduell.framework.gui.button.ButtonEvent;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;

	public class LevelUpPopup extends PyramidBasePopup {
		
		protected static const ITEM_BUTTON_OFFSET:int = 87;
		protected static const MAX_VISIBLE_ITEMS:int = 8;
		
		protected var scrollContainer:Sprite;
		protected var scrollLeftButton:Button;
		protected var scrollRightButton:Button;
		protected var itemCount:int = 0;
		protected var currentIndex:int = 0;
		protected var brick:Brick;
		protected var popupId:String;
		protected var noNewLevel:Boolean;
		
		public function LevelUpPopup(aBrick:Brick, popupLibraryId:String = "levelUpPopup", popupId:String = "levelUpPopup", skipInit:Boolean = false) {
			super(popupLibraryId, popupId, 0, true);
			noNewLevel = skipInit;
			popupId = popupLibraryId;
			brick = aBrick;
			scrollContainer = sprite.getChildByName("scrollContainer") as Sprite;
			scrollLeftButton = new Button(sprite.getChildByName("scrollLeftButton") as Sprite, "scrollLeftButton");
			scrollLeftButton.addEventListener(ButtonEvent.CLICK, onScrollLeft);
			scrollLeftButton.active = true;
			scrollRightButton = new Button(sprite.getChildByName("scrollRightButton") as Sprite, "scrollRightButton");
			scrollRightButton.addEventListener(ButtonEvent.CLICK, onScrollRight);
			scrollRightButton.active = true;
			
			if(!skipInit){
				localeData.setValue("brickCoinsNeeded", String(brick.coinsNeeded));
				var coinStatus:TextField = (sprite.getChildByName("progress") as Sprite).getChildByName("coinsText") as TextField;
				coinStatus.text = brick.coinsNeeded + " / " + brick.coinsNeeded;
			}
			initScroller();
		}

		private function showUnlockedBricks(newBricks:Array):void {
			var rewardContainer:Sprite = sprite.getChildByName("rewardContainer") as Sprite;
			var xPos:int = 0;
			for(var i:* in newBricks){
				var aBrickSprite:Sprite = pageManager.getPicFromLib(newBricks[i]); // library.createSprite(newBricks[i]);
				var scaleFactor:Number = 100 / aBrickSprite.width;
				rewardContainer.addChild(aBrickSprite);
				aBrickSprite.scaleX = scaleFactor;
				aBrickSprite.scaleY = scaleFactor;
				Debug.trace("showUnlockedBricks scaleFactor "+scaleFactor);
				if(xPos != 0){
					aBrickSprite.x = int(xPos * 90 + 20);
				}
				xPos++;
			}
			rewardContainer.x = int((sprite.width - rewardContainer.width) / 2);
		}

		protected function initScroller():void {
			var brickCatalog:Array = controller.gameData.pyramid.brickCatalog;
			var newBrickCatalog:Array = controller.gameData.pyramid.newBricks;
			
			var tempCatalog:Array = new Array();
			if(noNewLevel){
				tempCatalog = brickCatalog.concat();
			}else{
				tempCatalog = brickCatalog.concat(newBrickCatalog).concat();
				if(newBrickCatalog.length > 0){
					showUnlockedBricks(newBrickCatalog);
				}
				localeData.setValue("newbricks", String(newBrickCatalog.length));
			}
 			var currX:int = -1;

			if(tempCatalog.length < (MAX_VISIBLE_ITEMS + 2)){
				var diff:int = MAX_VISIBLE_ITEMS - tempCatalog.length + 2;
				for(var n:int = 0; n < diff; n++){
					tempCatalog.push("locked");
				}
			}
 			
			for(var i:int = 0;i < tempCatalog.length; i++) {
				var buttonId:String = tempCatalog[i];
				var brickCondition:BrickCondition = null;
				if(buttonId != "locked"){
					brickCondition = controller.gameData.pyramid.getBrickConditionbyType(tempCatalog[i]);
				}
				var brickSelectItem:BrickSelectItem = new BrickSelectItem(buttonId, brickCondition);
				brickSelectItem.addEventListener(ButtonEvent.CLICK, onSelectionDone);
				
				scrollContainer.addChild(brickSelectItem.sprite);
	
				brickSelectItem.sprite.scaleX = 0.80;
				brickSelectItem.sprite.scaleY = 0.80;
				
				if(i % 2 == 0){
					currX++;
					brickSelectItem.sprite.x = currX * ITEM_BUTTON_OFFSET;
				}else{
					brickSelectItem.sprite.x = currX * ITEM_BUTTON_OFFSET;
					brickSelectItem.sprite.y += ITEM_BUTTON_OFFSET + 3;
				}
				itemCount++;
			}
			currentIndex = itemCount;
			
			if(!noNewLevel){
				scrollToRightEnd(); // scroll to end to show the new items
			}
		}
		
		protected function scrollToRightEnd():void {
			var distance:int = int((itemCount - MAX_VISIBLE_ITEMS) / 2) - 1;
			for(var i:int = 0; i < distance; i++){
				onScrollRight();
			}
		}

		protected function onScrollLeft(event:Event = null):void {
			if((currentIndex) < itemCount) {
				disableButtons();
				var aTween:GDTween = new GDTween(scrollContainer, "x", Regular.easeInOut, scrollContainer.x, scrollContainer.x + ITEM_BUTTON_OFFSET, 0.2, true);
				aTween.addEventListener(TweenEvent.MOTION_FINISH, enableButtons);
				aTween.start();
				currentIndex+=2;
			}
		}

		protected function onScrollRight(event:Event = null):void {
			if((currentIndex - 2) > MAX_VISIBLE_ITEMS) {
				if(event != null){
					disableButtons();
					var aTween:GDTween = new GDTween(scrollContainer, "x", Regular.easeInOut, scrollContainer.x, scrollContainer.x - ITEM_BUTTON_OFFSET, 0.2, true);
					aTween.addEventListener(TweenEvent.MOTION_FINISH, enableButtons);
					aTween.start();
				}else{
					scrollContainer.x = scrollContainer.x - ITEM_BUTTON_OFFSET;
				}
				currentIndex-=2;
			}
		}

		protected function disableButtons(event:Event = null):void {
			scrollRightButton.active = false;
			scrollLeftButton.active = false;
		}

		protected function enableButtons(event:Event = null):void {
			scrollRightButton.active = true;
			scrollLeftButton.active = true;
		}

		protected function onSelectionDone(event:ButtonEvent):void {
			Debug.trace("LevelUpPopup::onSelectionDone: brick slected: " + event.button.id, Debug.DEBUG);
			brick.brickType = event.button.id;
			brick.state = Brick.STATE_COMPLETED;
			hide();
		}
	}
}
